<?php
namespace Home\Model;
//资源库
class Bucket extends Container{
    protected $user;
    protected $username;
    public function __construct($user){
        //数据集
        parent::__construct($user, "bucket");
    }

    public function initData(){
        //用户进来就有一个资源一的桶
        $this->resAddContainer('low','铁', 1);
    }

    //获取所有信息
    public function resLoginGet($needBox = true){
        $info = $this->hGetAll();
        $result = array();
        foreach(array('low', 'mid', 'height') as $level){
            foreach(array('铁', '木', '革', '草') as $i){
                //资源属性
                
                $result[$level.'_'.$i] = $this->getinfo($level, $i, -1);
            }
        }

        return $result;
    }

    public function getInfo($type_level, $type, $index = -1){
        $type_pre = $type_level.'_'.$type;
        $max = $this->getMax($type_level, $type);
        $city = new \Home\Model\Resource($user);
        $city = $city->getCityInfo($type_level.'_'.$type);
        $box = array();
        if($index == -1){
            for($j = 1; $j < 4; $j++){
                $tmp = $this->getBox($type_level, $type, $j);
                if($tmp) $box[] = $tmp;
            }
        }else if($index !=0){
            $tmp = $this->getBox($type_level, $type, $index);
            if($tmp) $box[] = $tmp;
        }   
        $result = array('current'=>$max['current'], 'max'=>$max['max'],
                        'city'=>$city, 'bucket'=>$box);
        return ;
    }
    public function getBox($type_level, $type, $index){
        $box_pre = $type_pre.'_'.$index;
        $tmp = $this->hMGet(array($box_pre.':level',
        $box_pre.':index',
        $box_pre.':capcity',
        $box_pre.':flash',
        $box_pre.':ts',
        $box_pre.':last_ts',
        $box_pre.':last_amount',
        $box_pre.':enable'
        ));
        if($tmp[$box_pre.':level']) return $tmp;
    }
    //升级箱子
    public function containerUpdate($type_level, $type, $index){
        $pre =$type_level.'_'.$type.'_'.$index.':';
        $type_pre = $type_level.'_'.$type;
        $info = $this->getInfo($type_level, $type, $index);
        $config = cfg('container');
        $box = $this->getBox($type_level, $type, $index);
        if(!box) return false;
        if($box[$pre.'level']>14) return false;
        $newLevel = $box[$pre.'level'] + 1;
        $inc = $config[$type_level]['capcity'][$newLevel] - $config[$type_level]['capcity'][$box[$pre.'level']];
        $this->up($type_level, $type, $inc);
        $this->hMset(array($pre.'level'=>$newLevel, $pre.'flash'=>$config[$type_level]['flash'][$newLevel]
                        ,$pre.'capcity'=>$config[$type_level]['capcity'][$newLevel]));
        return true;
    }
    
    ///添加资源桶
    public function resAddContainer($type_level, $type, $index, $ts = null){
        if($index > 3 || $index < 1 ) return error('资源桶只能有3个');//只能有3个
        if(!$ts)$ts = time();
        $config = cfg('container');
        $pre =$type_level.'_'.$type.'_'.$index.':';
        $type_pre = $type_level.'_'.$type;
        if(!$this->hSetNx($pre.'index', $index)) return error('改资源桶已存在');//判断是否已存在
        $shop = new \Home\Model\Shop($this->user);
        if(!$shop->add($pre, true)) return error('商店没有足够的空间');
        $shop->add($pre);
        $a = array(
            $pre.'type_level'=>$type_level,//资源等级
            $pre.'level'=>'0',//资源桶等级
            $pre.'capcity'=>$config[$type_level]['capcity'][0],
            $pre.'flash'=>$config[$type_level]['flash'][0],//桶里的最大数量
            $pre.'last_ts' => $ts,//中间结余时间
            $pre.'last_amount'=>$config[$type_level]['flash'][0],//中间结余数量
            $pre.'ts' => $ts,//上次采集时间,
            $pre.'index' => $index,
            $pre.'enable' => '1'
        );
        $this->hMset($a); 
        //总容量增加 
        $this->up($type_level, $type, $config[$type_level]['capcity'][0]);
        //初始化的桶里面是满的//自动收获一次
        $this->resGet($type_level, $type, $index, $ts);
        return $config[$type_level]['flash'][0];
    }

     //获取一个资源桶里的资源
     public function resGet($type_level, $type, $index, $ts = null){
        if(!$ts)$ts = time();
        $pre = $type_level.'_'.$type.'_'.$index.':';
        $type_pre = $type_level.'_'.$type;
        //满的， 直接返回
        $max = $this->getMax($type_level, $type);
        if($max['current'] >= $max['max']) return 0;
        $config = cfg('resource');
        $info = $this->hMGet(array($pre.'last_amount', 
                            $pre.'last_ts', $pre.'enable',
                            $pre.'level', $pre.'flash'));
        
        $city = new \Home\Model\Resource($this->user);
        $info = array_merge($info, $city->getCityInfo($type_pre));
        //如果没启用 或 数据错误 或 资源没有生产
        if(!$info || !$info[$pre.'enable'] || !$info[$pre.'last_ts'] || !is_numeric($info[$type_pre.':level']) || $info[$type_pre.':level'] < 0) return 0;
        
        $amount = $info[$pre.'last_amount'];
        $tmpts = $ts - $info[$pre.'last_ts'];
        //取出产量
        $rate = $config[$type_level]['rate'][intval($info[$type_pre.':level'])];
        if($info[$type_pre.':active'] == 1) $rate += $config[$type_level]['rewardRate'][intval($info[$type_pre.':level'])];

        //一小时产量除以小时数 //修复计时器误差
        $tmpamount = floor($rate * ($tmpts / 3600));
        $amount += $tmpamount;
        //不能大于桶的容量
        if($amount >= $info[$pre.'flash']){
            $amount = $info[$pre.'flash'];
            $tmpts = $ts;
        }else if($amount == 0){
            return 0;
        }else{
            $tmpts = $info[$pre.'last_ts'] + floor(($tmpamount * 3600) / $rate);
        }
        //加库存
        $tmpamount = $this->add($type_level, $type, $amount);
        $amount = $amount - $tmpamount;
        //更新时间,临时数量
        $this->hMset(array($pre.'ts'=>$tmpts, $pre.'last_ts'=>$tmpts, $pre.'last_amount'=>$amount));
        return $tmpamount;
    }

    public function resGetAll($type_level, $type, $ts){
        $amount = 0;
        for($i = 1; $i < 4; $i++){
            $amount += $this->resGet($type_level, $type, $i, $ts);    
        }
        return $amount;
    }

    public function up($type_level, $type, $amount){
        $this->hIncrBy($type_level.'_'.$type.'_max', $amount);
    }

    public function down($type_level, $type, $amount){
        $this->hIncrBy($type_level.'_'.$type.'_max', -$amount);
        $info = $this->getMax($type_level, $type);
        if($info['max']<$info['current'])
            $this->dec($type_level, $type, ($info['max']-$info['current']));
    }
    public function dec($type_level, $type, $amount, $pre = false){
        return parent::dec($type_level.'_'.$type, $amount, $pre);
    }
    //加
    public function add($type_level, $type, $amount){
       $info = $this->getMax($type_level, $type);
       if($info['max'] < $info['current'] + $amount)
            $amount = $info['max'] - $info['current'];
       if($amount == 0) return 0;
       parent::add($type_level.'_'.$type, $amount);
       return $amount;
    }
    
    public function getMax($type_level, $type){
        $info = $this->hMGet(array($type_level.'_'.$type, $type_level.'_'.$type.'_max'));
        return array('max'=>$info[$type_level.'_'.$type.'_max'] ?: 0, 'current'=>$info[$type_level.'_'.$type] ?: 0);
    }
    
    //临时结余一个资源桶里的资源//只会被城市调用
    public function resSave($type_level, $type, $index, $ts, $cityinfo){
        if(!$ts)$ts = time();
        $pre = $type_level.'_'.$type.'_'.$index.':';
        $type_pre = $type_level.'_'.$type;
        $config = cfg('resource');
        $info = $this->hMGet(array($pre.'last_amount', 
                            $pre.'last_ts', $pre.'enable',
                            $pre.'level', $pre.'flash'));
        $info = array_merge($info, $cityinfo);
        //如果没启用 或 数据错误 或 资源没有生产
        if(!$info || !$info[$pre.'enable'] || !$info[$pre.'last_ts'] || !is_numeric($info[$type_pre.':level']) || $info[$type_pre.':level'] < 0) return 0;
        //满的， 直接返回
        if($info[$pre.'last_amount'] >= $info[$pre.'flash']) return 0;
        $amount = $info[$pre.'last_amount'];
        $tmpts = $ts - $info[$pre.'last_ts'];
        //取出产量
        $rate = $config[$type_level]['rate'][intval($info[$type_pre.':level'])];
        if($info[$type_pre.':active'] == 1) $rate += $config[$type_level]['rewardRate'][intval($info[$type_pre.':level'])];

        //一小时产量除以小时数 //修复计时器误差
        $tmpamount = floor($rate * ($tmpts / 3600));
        $amount += $tmpamount;
        //不能大于桶的容量
        if($amount >= $info[$pre.'flash']){
            $amount = $info[$pre.'flash'];
            $tmpts = $ts;
        }else{
            $tmpts = $info[$pre.'last_ts'] + floor(($tmpamount * 3600) / $rate);
        }
        //更新时间,临时数量
        $this->hMset(array($pre.'last_ts'=>$tmpts, $pre.'last_amount'=>$amount));
        return $amount;
    }
    //只会被城市调用
    public function resSaveAll($type_level, $type, $ts, $cityinfo){
        $amount = 0;
        for($i = 1; $i < 4; $i++){
            $amount += $this->resSave($type_level, $type, $i, $ts, $cityinfo);    
        }
        return $amount;
    }
} 